<?php
/**
 * @title 767. 重构字符串
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param String $S
     * @return String
     */
    function reorganizeString($S) {
        /**
         * @since 2020-11-30 长度
         */
        $len = strlen($S);

        /**
         * @since 2020-11-30 计数
         */
        $counts = [];
        for($i=0; $i<$len; $i++){
            if(isset($counts[$S[$i]])){
                $counts[$S[$i]][1] += 1;
            } else {
                $counts[$S[$i]] = [$S[$i], 1];
            }
        }

        /**
         * @since 2020-12-01 按照数量排序
         */
        $nums = array_column($counts, 1);
        array_multisort($nums, SORT_ASC, SORT_NUMERIC, $counts);
        // return $counts;

        /**
         * @since 2020-11-30 一半上限, 超过剪枝
         */
        $half = $len>>1;
        if($len%2 == 1){
            $half++;
        } else {}
        // return $half;

        /**
         * @since 2020-11-30 递归找解
         */
        $result = self::find([], 0, $counts, $half);
        // return $result;

        /**
         * @since 2020-12-01 无解
         */
        if($result === false){
            return "";
        } else {}

        /**
         * @since 2020-12-01 2个字符串组
         */
        $str1 = $str2 = "";
        foreach ($counts as list($char, $num)){
            if(isset($result[$char])){
                $str1 .= str_repeat($char, $num);
            } else {
                $str2 .= str_repeat($char, $num);
            }
        }
        // return [$str1, $str2];

        /**
         * @since 2020-12-01 长度
         * @since 2020-12-01 字符串1长度 >= 字符串2长度
         */
        $len1 = strlen($str1);
        $len2 = strlen($str2);

        /**
         * @since 2020-12-01 组合字符串
         */
        $str = "";
        for($i=0; $i<$len2; $i++){
            $str .= $str1[$i] . $str2[$i];
        }

        /**
         * @since 2020-12-01 收官
         */
        if($i < $len1){
            $str .= $str1[$i];
        } else {}

        /**
         * @return
         */
        return $str;
    }

    /**
     * @title 找解
     * @author start2004
     * @since 2020-11-30 Created
     *
     * @param array $result
     * @param int $sum
     * @param array $counts
     * @param int $half
     */
    function find($result = [], $sum = 0, $counts = [], $half = 0){
        /**
         * @since 2020-11-30 尾部弹出一个数
         */
        if($counts){
            list($char, $num) = array_pop($counts);
        } else {
            return false;
        }

        if($sum+$num <= $half){
            /**
             * @since 2020-11-30 找到解
             */
            if($sum+$num === $half){
                $result[$char] = $num;
                return $result;
            } else {
                /**
                 * @since 2020-11-30 在队列, 是否有解
                 */
                $result[$char] = $num;
                $sum += $num;
                $data = self::find($result, $sum, $counts, $half);
                if($data !== false){
                    return $data;
                } else {}

                /**
                 * @since 2020-11-30 不在队列里, 是否有解
                 */
                unset($result[$char]);
                $sum -= $num;
                return self::find($result, $sum, $counts, $half);
            }
        } else {
            /**
             * @since 2020-11-30 溢出, 只能抛弃
             */
            return self::find($result, $sum, $counts, $half);
        }
    }
}


/**
 * @url http://127.0.0.1/leetcode/202011/2020.11.30.php
 */
$datas = [
    ["aab"],
    ["aaabbcdddddeeee"],
    ["aaab"],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->reorganizeString(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
